from sweetest.autotest import Autotest
from sweetest.send import SendFeishu
import time
import subprocess
import pyautogui
# 环境配置信息
# Http api
desired_caps = {'platformName': 'api'}
server_url = ''

# 创建一个字典来映射项目名称到相应的 plan_name 和 sheet_names
project_config = {
	"LanTu_test": {
		"plan_name": "LanTu_api_test",
		"sheet_names": ['数据采集', '事件采集', '历史数据采集', '默认上传数据', '信号分组管理']
		# "sheet_names": ['数据采集'],
	},
	"LanTu_pro": {
		"plan_name": "LanTu_api_pro",
		# "sheet_names": ['海豚定时调度']
		"sheet_names": ['调试3']
	},
	"Geely_test": {
		"plan_name": "Geely_api_test",
		# "sheet_names": ['普通采集', '事件采集']
		"sheet_names": ['普通采集', '事件采集']
	},
}


def main(project_name):
	# 根据项目名称选择相应的配置
	project_configurations = project_config.get(project_name)
	plan_name = project_configurations.get('plan_name', {})
	sheet_name = project_configurations.get('sheet_names', {})

	# 初始化自动化实例
	sweet = Autotest(plan_name, sheet_name, desired_caps, server_url)

	# 按条件执行,支持筛选的属性有：'id', 'title', 'designer', 'priority'
	# sweet.fliter(priority='H')

	# 执行自动化测试
	sweet.plan()

	# # 推送测试报告
	faicase, blocase = sweet.case_group_cou()
	sc, fc, bc, tc = sweet.report('markdown')

	send = SendFeishu(sweet.code, plan_name, sheet_name, faicase, blocase, sc, fc, bc, tc)
	# send.run()
	# send.sendmessage_feishu()
	send.run_finnal()
	send.send_file()

def link_vpn():

	# 获取系统当前代码页
	encoding_output = subprocess.check_output('chcp', shell=True)
	# chcp命令的输出通常是"Active code page: XXXX\n"，我们需要提取XXXX
	encoding_info = encoding_output.decode('utf-8', 'ignore').split(':')[-1].strip()
	system_encoding = int(encoding_info)
	# 获取系统编码
	encoding = 'cp' + str(system_encoding)
	# 使用 netsh 命令获取指定适配器的状态
	command = f'netsh interface show interface name="以太网 3"'

	while True:
		output = subprocess.check_output(command, shell=True).decode(encoding)
		# 检查输出中是否有 "已连接" 关键字
		if '已连接' not in output:
			print(f"岚图VPN 未连接，正在尝试重新连接......")
			# 启动 VPN 客户端
			vpn_process = subprocess.Popen([r"C:\Program Files\Fortinet\FortiClient\FortiClient.exe"])
			# 等待 VPN 客户端加载
			time.sleep(10)
			# 使用 pyautogui 模拟点击连接按钮
			pyautogui.click(x=520, y=400)  # 请替换为连接按钮的实际位置
			time.sleep(5)

			for i in range(4):
				pyautogui.press('tab')
				time.sleep(2)
			pyautogui.press('enter')
			continue
		else:
			print("岚图VPN 已连接")
			break


# 如果是集成到 CI/CD，则给出退出码；也可以根据上面的测试结果自己生成退出码
# sys.exit(sweet.code)

if __name__ == '__main__':
	# link_vpn()
	# main("La  nTu_test")
	main("LanTu_pro")
	# main("Geely_test")
